home *** CD-ROM | disk | FTP | other *** search
/ Eagles Nest BBS 8 / Eagles_Nest_Mac_Collection_Disc_8.TOAST / Developer Tools⁄Additions / IntermediatC / Ships 2 #8 / ships 2.c next >
C/C++ Source or Header  |  1990-11-02  |  7KB  |  237 lines

  1. /*
  2.  
  3. GIVEN:
  4.  
  5. #include <math.h>
  6.  
  7. #define RADIANS_PER_DEGREE    0.017453292519943
  8.  
  9. typedef struct ship_type
  10.    {
  11.    int hull_number;
  12.    position_type posit;
  13.    double course;
  14.    double speed;
  15.    } ship_type;
  16.  
  17.  
  18. CREATE THIS PROGRAM:
  19.  
  20. Create NO_OF_SHIPS (define anywhere from 10 to 20) at random positions -100 to 100
  21. (longitude and latitude will be of type double) with randomly assigned courses from
  22. 0 up to and including 359 degrees and speed from 10 to 30 knots.  North is 0 degrees,
  23. and degrees increase clockwise (a diagram is provided below).  Assign the hull number
  24. any way you desire, but all ships must have a different hull number.  Display all
  25. ship information.  This includes the hull number, position (longitude and latitude),
  26. course, and speed.
  27.  
  28. Now, let the ships sail (they move the assigned speed in the assigned direction).
  29. Ignore units (thus, a ship at 0 longitude and 0 latitude with a course of 180 and
  30. speed of 15 would move 15 south to 0 longitude and -15 latitude).  The ships are to
  31. each move 5 times.  After each set of moves, eliminate any ship that is no longer in
  32. the region of -100 to 100 longitude and -100 to 100 latitude.  Thus NO_OF_SHIPS are
  33. to move, the program will search to see if any ships are no longer in the boundaries,
  34. eliminate tracking those ships in the future, and proceed to the next set of movements.
  35. As said earlier, the ships each move 5 times (unless no longer being tracked).
  36. Those ships still in the boundaries after 5 moves are to be listed as above in the same
  37. file.
  38.  
  39.  
  40.                                     DIAGRAM OF DIRECTIONS
  41.  
  42.                                             North
  43.  
  44.                                             0 degrees
  45.                                             |
  46.                                             |
  47.                                             |
  48.                     West    270 degrees    --------- 90 degrees    East
  49.                                             |
  50.                                             |
  51.                                             |
  52.                                            180 degrees
  53.  
  54.                                             South
  55.  
  56. */
  57.  
  58.  
  59. /* ------------------------------------------------------------------------------- */
  60.  
  61.  
  62. #include <stdio.h>
  63. #include <stdlib.h>
  64. #include <math.h>
  65.  
  66.  
  67. /* ------------------------------------------------------------------------------- */
  68.  
  69.  
  70. #define random(x,y)            (rand() % ((y) - (x) + 1) + (x))
  71. #define RADIANS_PER_DEGREE    0.017453292519943 /* conversion factor for degrees to radians */
  72. #define TRACKING            0xff
  73. #define NOT_TRACKING        0x00
  74. #define NO_OF_SHIPS            12        /* maximum number of ships */
  75. #define SHIP_HOURS            5        /* number of times ships move */
  76. #define MIN_POSITION        -100    /* min and max positions, courses and speeds */
  77. #define MAX_POSITION        100
  78. #define MIN_COURSE            0
  79. #define MAX_COURSE            359
  80. #define MIN_SPEED            10
  81. #define MAX_SPEED            30
  82. #define INITIAL_SHIP_INFO    0xff    /* used to determine header of output file */
  83. #define FINAL_SHIP_INFO        0x00
  84.  
  85.  
  86. /* ------------------------------------------------------------------------------- */
  87.  
  88.  
  89. typedef struct position_type
  90.    {
  91.    double longitude;
  92.    double latitude;
  93.    } position_type;
  94.  
  95. typedef struct ship_type
  96.    {
  97.    int hull_number;
  98.    position_type posit;
  99.    double course;
  100.    double speed;
  101.    } ship_type;
  102.  
  103.  
  104. /* ------------------------------------------------------------------------------- */
  105.  
  106.  
  107. static void initialize_ships( ship_type ships[], char tracking[], int no_of_ships );
  108. static void move_ships( ship_type ships[], char tracking[], int no_of_ships );
  109. static void display_ship_info( ship_type ships[], char tracking[], 
  110.     int no_of_ships, FILE *file_stream, char output_type );
  111. static void find_new_position( ship_type ships[], int sub );
  112. static int check_if_still_track( ship_type ships[], int sub );
  113.  
  114.  
  115. /* ------------------------------------------------------------------------------- */
  116.  
  117.  
  118. main()
  119.    {
  120.    ship_type    ships[NO_OF_SHIPS];
  121.    char            tracking[NO_OF_SHIPS];
  122.    int            i;
  123.    FILE         *file_stream;
  124.  
  125.    initialize_ships( ships, tracking, NO_OF_SHIPS );
  126.    file_stream = fopen("ships.dat", "w");
  127.    display_ship_info( ships, tracking, NO_OF_SHIPS, file_stream, INITIAL_SHIP_INFO );
  128.    for (i=0; i<SHIP_HOURS; i++)
  129.       move_ships( ships, tracking, NO_OF_SHIPS );
  130.    display_ship_info( ships, tracking, NO_OF_SHIPS, file_stream, FINAL_SHIP_INFO );
  131.    fclose(file_stream);
  132.    }
  133.  
  134.  
  135. /* ------------------------------------------------------------------------------- */
  136.  
  137.  
  138. static void initialize_ships( ship_type ships[], char tracking[], int no_of_ships )
  139.    {
  140.    int i;
  141.  
  142.    /*
  143.       Initialize the tracking array to track all ships.  Set the hull number by the
  144.       given formula, and get position, course and speed of each ship by random function.
  145.    */
  146.    for (i=0; i<no_of_ships; i++)
  147.       {
  148.       tracking[i] = TRACKING;
  149.       ships[i].hull_number = (i + 1) * 10;
  150.       ships[i].posit.longitude = (double) random(MIN_POSITION, MAX_POSITION);
  151.       ships[i].posit.latitude = (double) random(MIN_POSITION, MAX_POSITION);
  152.       ships[i].course = (double) random(MIN_COURSE, MAX_COURSE);
  153.       ships[i].speed = (double) random(MIN_SPEED, MAX_SPEED);
  154.       }
  155.    }
  156.  
  157.  
  158. /* ------------------------------------------------------------------------------- */
  159.  
  160.  
  161. static void move_ships( ship_type ships[], char tracking[], int no_of_ships )
  162.    {
  163.    int i;
  164.  
  165.    for (i=0; i<no_of_ships; i++)
  166.      if (tracking[i])                /* if ship still being tracked, get new position */
  167.         {
  168.         find_new_position( ships, i );
  169.         if (!(check_if_still_track( ships, i )))    /* check if ship in boundaries */
  170.            tracking[i] = NOT_TRACKING;    /* turn tracking of ship off if not in boundaries */
  171.         }
  172.    }
  173.  
  174.  
  175. /* ------------------------------------------------------------------------------- */
  176.  
  177.  
  178. static void find_new_position( ship_type ships[], int sub )
  179.    {
  180.    /*
  181.       Since course starts at north (0 degrees), and increases clockwise,
  182.       the sine of the angle multiplied by distance will give the longitude
  183.       component, while the cosine gives the latitude distance.
  184.    */
  185.    ships[sub].posit.longitude += (sin(ships[sub].course * RADIANS_PER_DEGREE) *
  186.       ships[sub].speed);
  187.    ships[sub].posit.latitude += (cos(ships[sub].course * RADIANS_PER_DEGREE) *
  188.       ships[sub].speed);
  189.    }
  190.  
  191.  
  192. /* ------------------------------------------------------------------------------- */
  193.  
  194.  
  195. static int check_if_still_track( ship_type ships[], int sub )
  196.    {
  197.    /*
  198.       Check to see if longitude or latitude is outside of boundaries.  If so, return
  199.       a NO_TRACKING, else return TRACKING.
  200.    */
  201.    if ((ships[sub].posit.longitude < (double) MIN_POSITION) ||
  202.        (ships[sub].posit.longitude > (double) MAX_POSITION) ||
  203.        (ships[sub].posit.latitude < (double) MIN_POSITION) ||
  204.        (ships[sub].posit.latitude > (double) MAX_POSITION))
  205.       return NOT_TRACKING;
  206.    else
  207.       return TRACKING;
  208.    }
  209.  
  210.  
  211. /* ------------------------------------------------------------------------------- */
  212.  
  213.  
  214. static void display_ship_info( ship_type ships[], char tracking[], 
  215.     int no_of_ships, FILE *file_stream, char output_type )
  216.    {
  217.    int i;
  218.    
  219.    /*
  220.       The output of both initial and final data uses this procedure.  The parameter
  221.       output_type tells the computer what title to give each output set.
  222.    */
  223.  
  224.    if (output_type)
  225.       fprintf(file_stream, "Initial Ship Information:\n\n");
  226.    else
  227.       fprintf(file_stream, "\n\n\nFinal Ship Information:\n\n");
  228.    fprintf(file_stream, "Ship  Hull Number  Longitude  Latitude  Course  Speed\n");
  229.    for (i=0; i<no_of_ships; i++)
  230.       if (tracking[i])                /* print only if still in boundaries */
  231.          {
  232.          fprintf(file_stream, "%4d%9d%13.2lf%11.2lf%8.0lf%7.0lf\n", i,
  233.             ships[i].hull_number, ships[i].posit.longitude, ships[i].posit.latitude,
  234.             ships[i].course, ships[i].speed);
  235.          }
  236.    }
  237.